package com.ha.garbage;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.ha.garbage.context.BaseContext;
import com.ha.garbage.model.Result;
import com.ha.garbage.model.UserPasswordDTO;
import com.ha.garbage.service.userService;
import com.ha.garbage.utils.UserRetrofit;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ModifyActivity extends AppCompatActivity {

    private Button btn_confirm;
    private Button btn_cancel;
    private EditText tv_pwd;
    private EditText tv_pwd1;
    private EditText tv_pwd2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_modify);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        tv_pwd = findViewById(R.id.tv_modify_password);
        tv_pwd1 = findViewById(R.id.tv_modify_password1);
        tv_pwd2 = findViewById(R.id.tv_modify_password2);


        btn_cancel = findViewById(R.id.btn_password_cancel);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_confirm = findViewById(R.id.btn_password_confirm);
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(tv_pwd.getText().toString().isEmpty() || tv_pwd1.getText().toString().isEmpty()
                        || tv_pwd2.getText().toString().isEmpty()){
                    new AlertDialog.Builder(v.getContext())
                            .setMessage("输入格式有误重新输入")
                            .setPositiveButton("知道了",null)
                            .show();}
                    if(!tv_pwd1.getText().toString().equals( tv_pwd2.getText().toString()) ){
                        new AlertDialog.Builder(v.getContext())
                                .setMessage("两次输入的密码不一样")
                                .setPositiveButton("知道了",null)
                                .show();
                    }
                    Retrofit retrofit = UserRetrofit.retrofit();
                    userService userService = retrofit.create(userService.class);


                    if(BaseContext.getCurrentId()!= null){
                        String token = BaseContext.getCurrentToken();
                        Call<Result> resultCall = userService.modifyPassword(new UserPasswordDTO(BaseContext.getCurrentId(),
                                tv_pwd.getText().toString(), tv_pwd1.getText().toString()),token);
                        resultCall.enqueue(new Callback<Result>() {
                            @Override
                            public void onResponse(Call<Result> call, Response<Result> response) {
                                if(response.isSuccessful()){
                                    Result result = response.body();
                                    if(result.getCode() == 1){
                                        new AlertDialog.Builder(v.getContext())
                                                .setMessage("修改密码成功")
                                                .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        finish();
                                                    }
                                                })
                                                .show();
                                    }else {
                                        new AlertDialog.Builder(v.getContext())
                                                .setMessage(result.getMsg())
                                                .setPositiveButton("知道了",null)
                                                .show();
                                    }
                                }
                            }

                            @Override
                            public void onFailure(Call<Result> call, Throwable t) {

                                System.out.println("请求路径为："+call.request().url().toString());
                                System.out.println("请求路径为："+t.toString());
                            }
                        });
                    }else {
                        new AlertDialog.Builder(v.getContext())
                                .setMessage("请登录后重试")
                                .setPositiveButton("知道了",null)
                                .show();
                    }

                }

        });
    }
}